#!/bin/bash
# - master_threads
# - agent_threads
# - runtime

. "$LKP_SRC/lib/reproduce-log.sh"

: "${runtime:=300}"

[ -n "$direct_server_ips" ] && server=$direct_server_ips
[ -z "$server" ] && {
	if [ "$ip" = 'ipv6' ]; then
		server=::1
	else
		server=127.0.0.1
	fi
}
server=${server%% *}


set_opt()
{
	opt=
	[ -n "$agent_threads" ] && opt="${opt} -a localhost"
}

run_agent()
{
	if [ -n "$agent_threads" ] && [ "$agent_threads" -ne "0" ]; then
		log_bg_cmd mutilate -T "$agent_threads" -A
		agent_pid=$!
	fi
}

run_mutilate()
{
	log_cmd mutilate -T "$master_threads" -s "$server" -t "$runtime" "$opt"
}

# Increase the number of files that the process allows to open,
# to avoid `Too many open files` error when there are too many threads.
ulimit -n 4096
set_opt
run_agent
run_mutilate

[ -n "$agent_pid" ] && kill "$agent_pid"

exit 0
